"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 兼容性
Case Name   : 修改成功后查看审计日志中是否对密码进行脱敏
Create At   : 2023/06/06
@zou_jialiang0501129488
Description :
    1、创建兼容b库并查询插件是否加载成功
    2、执行grant后查看审计日志中是否有明文密码
    3、清理环境
Expect      :
    1、成功
    2、审计日志中无明文显示密码
    3、成功
History     :
"""
import os
import re
import time
import unittest

from yat.test import macro

from testcase.utils.Common import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_grant_case021'
        self.u_name = 'u_grant_case021'

    def test_grant(self):
        text = '-----step1:创建兼容b库库并查询插件;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility ='B';"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS,
                      sql_res, '兼容B库创建失败')

        sql_cmd1 = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd1)
        sql_res1 = self.pri_sh.execut_db_sql(sql_cmd1, dbname=self.db_name)
        self.log.info(sql_res1)
        self.assertIn('dolphin', sql_res1, '查询失败')

        text = '-----step2:执行grant语句后查看审计日志检查是否加密;expect:无明文密码显示-----'
        self.log.info(text)
        sql_cmd = f"select now();" \
                  f"drop user if exists {self.u_name} cascade;"
        sql_res = self.pri_sh.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, sql_res, '创建用户失败')

        time_begin = re.findall('\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}',
                                sql_res)[0]
        self.log.info(time_begin)
        sql_cmd1 = f"grant usage on *.* to {self.u_name} " \
                   f"identified by '{macro.COMMON_PASSWD}';" \
                   f"grant usage on *.* to {self.u_name} " \
                   f"identified by '{macro.PASSWD_INITIAL}';"
        sql_res1 = self.pri_sh.execut_db_sql(sql_cmd1, dbname=self.db_name)
        self.log.info(sql_res1)
        self.assertEqual(2, sql_res1.count(
            self.constant.ALTER_ROLE_SUCCESS_MSG), 'grant语句执行失败')
        time.sleep(5)

        sql_cmd2 = f"select * from pg_query_audit('{time_begin}',now()) " \
                   f"where detail_info like '%grant%';"
        sql_res2 = self.pri_sh.execut_db_sql(sql_cmd2, dbname=self.db_name)
        self.log.info(sql_res2)
        self.assertNotIn(macro.COMMON_PASSWD, sql_res2, 'grant语句明文显示密码')
        self.assertNotIn(macro.PASSWD_INITIAL, sql_res2, 'grant语句明文显示密码')

    def tearDown(self):
        text = "-----step3:清理环境;expect:成功-----"
        self.log.info(text)
        sql_cmd3 = f"drop database if exists {self.db_name};" \
                   f"drop user if exists {self.u_name} cascade;"
        sql_res3 = self.pri_sh.execut_db_sql(sql_cmd3)
        self.log.info(sql_res3)
        self.log.info(f'-----{os.path.basename(__file__)} finish-----')
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, sql_res3, '用户删除失败')
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_res3, '数据库删除失败')
